package com.example.community.actuator;

import com.example.community.util.CommunityUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author: Ctc
 * @Date: 2022/4/21 19:03
 */
//项目监控
@Component
@Endpoint(id = "database")
public class DatebaseEndpoint {
    private static final Logger logger = LoggerFactory.getLogger(DatebaseEndpoint.class);
    @Autowired
    private DataSource dataSource;

    @ReadOperation
    public String checkConnection() {
        try (Connection conn = dataSource.getConnection()) {
            return CommunityUtil.getJSONString(0, "获取连接成功");
        } catch (SQLException e) {
            logger.error("获取链接失败");
            return CommunityUtil.getJSONString(1, "获取连接失败！");
        }
    }
}